package org.example.module_login.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.module_login.dao.TeacherDao;
import org.example.module_login.entity.Teacher;
import org.example.module_login.service.TeacherService;
import org.example.module_login.utlis.CreateAccountNumber;
import org.example.module_login.utlis.Encryption;
import org.example.module_login.utlis.JWTToken;
import org.example.module_login.utlis.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;

/**
 * 教师表(Teacher)表服务实现类
 *
 * @author HAJ
 * @since 2025-03-03 15:14:34
 */
@Service("teacherService")
public class TeacherServiceImpl extends ServiceImpl<TeacherDao, Teacher> implements TeacherService {

    @Autowired
    public TeacherDao dao;

    /**
     * 教师账号密码登录
     */
    @Override
    public R login(Teacher teacher, HttpServletResponse response) {
        QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
        wrapper.eq("teacher_id", teacher.getTeacherId());
        List<Teacher> teacherList = dao.selectList(wrapper);
        if (teacherList.isEmpty()) {
            return R.error("账号不存在！");
        } else {
            Teacher teacher1 = teacherList.get(0);
            if(Encryption.loginUser(teacher.getPassword(),teacher1.getPassword(),teacher1.getSalt())){
                String token = JWTToken.createToken(teacherList.get(0).getName());
                // 设置 HttpOnly Cookie
                Cookie cookie = new Cookie("Authorization",token);
                cookie.setHttpOnly(true);
                // cookie.setSecure(true); // 确保通过 HTTPS 传输
                cookie.setMaxAge(60);
                cookie.setPath("/");
                response.addCookie(cookie);
                return R.ok("登录成功!");
            }else {
                return R.error("账号或密码错误!");
            }
        }
    }

    /**
     * 注册
     */
    @Override
    public R register(Teacher teacher) {
        String teacherId = createCreateAccountNumber();
        String salt = Encryption.generateSalt();
        System.out.println(teacherId);
        Teacher newTeacher = new Teacher();
        newTeacher.setName(teacher.getName());
        newTeacher.setTeacherId(teacherId);
        newTeacher.setPassword(Encryption.registerUser(teacher.getPassword(),salt));
        newTeacher.setSex(teacher.getSex());
        newTeacher.setSalt(salt);
        try {
            dao.insert(newTeacher);
            HashMap<String,String> returnInfo = new HashMap<>();
            returnInfo.put("teacherId",teacherId);
            returnInfo.put("msg","注册成功!");
            return R.data(returnInfo);
        }catch (Exception e){
            return R.error("错误，稍后再试！");
        }
    }

    /**
     * 账号存在判断
     * */
    public String createCreateAccountNumber(){
        while (true){
            String teacherId = CreateAccountNumber.createAccountNumber();
            QueryWrapper<Teacher> wrapper = new QueryWrapper<>();
            wrapper.eq("teacher_id",teacherId);
            if(dao.selectList(wrapper).isEmpty()){
                return teacherId;
            }
        }
    }

}

